﻿$(document).ready(function () {
    setButtonEvent();
    $("#Button_SearchClient").css({ "display": "block" });
    $("#dashBoard").addClass("active");
    SetContentPosition();
    SetScroll($(".content"));
    windowWidth = parseInt($(window).width());
    
});

window.onresize = function (event) {
    SetContentPosition();
}

function SetScroll(element) {
    $(element).mCustomScrollbar({
        scrollButtons: {
            enable: true
        }
    });
}

function ShowPopup(element) {
    $("#popupBg").fadeIn();
    $("#" + element).fadeIn();
}

function ShowLoader() {
    $("#popupLoader").fadeIn();
    $("#popupBgLoader").fadeIn();
}

function HideLoader() {
    $("#popupLoader").fadeOut();
    $("#popupBgLoader").fadeOut();
}


function ResetInput(contList) {
    for (var k = 0; k < contList.length; k++) {
        var Container = contList[k];
        var InputTextList = $(Container).find(":text");
        var SelectList = $(Container).find("select");
        for (var i = 0; i < InputTextList.length; i++) {
            var initialValue = $(InputTextList[i]).attr("data");
            $(InputTextList[i]).val(initialValue);
            if ($(InputTextList[i]).attr("class").indexOf("invalid") != -1) {
                var padL = parseInt($(InputTextList[i]).css("padding-left")) + 1;
                var padR = parseInt($(InputTextList[i]).css("padding-right")) + 1;
                var padT = parseInt($(InputTextList[i]).css("padding-top")) + 1;
                var padB = parseInt($(InputTextList[i]).css("padding-bottom")) + 1;
                $(InputTextList[i]).css({ "padding-left": padL + "px", "padding-right": padR + "px", "padding-top": padT + "px", "padding-bottom": padB + "px", "border-color": "#9C9C9C" });
                $(InputTextList[i]).removeClass("invalid");
            }
        }
        for (var i = 0; i < SelectList.length; i++) {
            var initialValue = $(SelectList[i]).attr("data");
            $(SelectList[i]).val(initialValue);
            if ($(SelectList[i]).attr("class").indexOf("invalid") != -1) {
                var padL = parseInt($(SelectList[i]).css("padding-left")) + 1;
                var padR = parseInt($(SelectList[i]).css("padding-right")) + 1;
                var padT = parseInt($(SelectList[i]).css("padding-top")) + 1;
                var padB = parseInt($(SelectList[i]).css("padding-bottom")) + 1;
                $(SelectList[i]).css({ "padding-left": padL + "px", "padding-right": padR + "px", "padding-top": padT + "px", "padding-bottom": padB + "px", "border-color": "#9C9C9C" });
                $(SelectList[i]).removeClass("invalid");
            }
        }
    }
}

function ValidateControlList(Container) {
    var InputTextList = $(Container).find(":text");
    var SelectList = $(Container).find("select");
    var valid = true;
    for (var i = 0; i < InputTextList.length; i++) {
        var initialValue = $(InputTextList[i]).attr("data");
        if ($(InputTextList[i]).val() == initialValue || $(InputTextList[i]).val() == "") {
            if ($(InputTextList[i]).attr("class").indexOf("invalid") == -1) {
                var padL = parseInt($(InputTextList[i]).css("padding-left")) - 1;
                var padR = parseInt($(InputTextList[i]).css("padding-right")) - 1;
                var padT = parseInt($(InputTextList[i]).css("padding-top")) - 1;
                var padB = parseInt($(InputTextList[i]).css("padding-bottom")) - 1;
                $(InputTextList[i]).css({ "padding-left": padL + "px", "padding-right": padR + "px", "padding-top": padT + "px", "padding-bottom": padB + "px", "border-color": "#CC0833" });
                $(InputTextList[i]).addClass("invalid");
                valid = false;
            }
        }
        else {

            if ($(InputTextList[i]).attr("class").indexOf("invalid") != -1) {
                var padL = parseInt($(InputTextList[i]).css("padding-left")) + 1;
                var padR = parseInt($(InputTextList[i]).css("padding-right")) + 1;
                var padT = parseInt($(InputTextList[i]).css("padding-top")) + 1;
                var padB = parseInt($(InputTextList[i]).css("padding-bottom")) + 1;
                $(InputTextList[i]).css({ "padding-left": padL + "px", "padding-right": padR + "px", "padding-top": padT + "px", "padding-bottom": padB + "px", "border-color": "#9C9C9C" });
                $(InputTextList[i]).removeClass("invalid");
                valid = true;
            }
        }
    }
    for (var i = 0; i < SelectList.length; i++) {
        var initialValue = $(SelectList[i]).attr("data");
        if ($(SelectList[i]).val() == initialValue) {
            if ($(SelectList[i]).attr("class").indexOf("invalid") == -1) {
                var padL = parseInt($(SelectList[i]).css("padding-left")) - 1;
                var padR = parseInt($(SelectList[i]).css("padding-right")) - 1;
                var padT = parseInt($(SelectList[i]).css("padding-top")) - 1;
                var padB = parseInt($(SelectList[i]).css("padding-bottom")) - 1;
                $(SelectList[i]).css({ "padding-left": padL + "px", "padding-right": padR + "px", "padding-top": padT + "px", "padding-bottom": padB + "px", "border-color": "#CC0833" });
                $(SelectList[i]).css("invalid");
                valid = false;
            }
        }
        else {
            if ($(SelectList[i]).attr("class").indexOf("invalid") != -1) {
                var padL = parseInt($(SelectList[i]).css("padding-left")) + 1;
                var padR = parseInt($(SelectList[i]).css("padding-right")) + 1;
                var padT = parseInt($(SelectList[i]).css("padding-top")) + 1;
                var padB = parseInt($(SelectList[i]).css("padding-bottom")) + 1;
                $(SelectList[i]).css({ "padding-left": padL + "px", "padding-right": padR + "px", "padding-top": padT + "px", "padding-bottom": padB + "px", "border-color": "#9C9C9C" });
                $(SelectList[i]).removeClass("invalid");
                valid = true;
            }
        }
    }
    return valid;
}

function SetConfirm(userType, confirmMessage) {
    $("#confirmMessage").html(confirmMessage);
    $("#Button_ConfirmDeleteId").unbind("click");
    $("#Button_ConfirmDeleteId").click(function () {
        if (userType == "customer") {
            DeleteCustomer();
        }
        if (userType == "customerLovs") {
            DeleteCustomerLovs();
        }
        if (userType == "client") {
            DeleteClient();
        }
        if (userType == "clientLovs") {
            DeleteClientLovs();
        }
    });
    $("#Button_CancelPopup").unbind("click");
    $("#Button_CancelPopup").click(function () {
        $(".pop").fadeOut();
    });
    ShowPopup("popupConfirm");
}

function setButtonEvent() {
    $(".optbutton").click(function () {
        var OptList = $(this).find(".optionUl")
        var visible = $(OptList).css("display");
        if (visible == "none") {
            $(".optionUl").css({ "display": "none" });
            $(OptList).css({ "display": "block" });
        }
        else {
            $(OptList).css({ "display": "none" });
        }
    });
    $(".optItem").click(function () {
        var datval = $(this).attr("data");
        if (datval.indexOf("pop") != -1) {
            $("#popupBg").fadeIn();
            $("#" + datval).fadeIn();
            GetCustomerList();
        }
        else {
            $(".searchButton").css({ "display": "none" });
            $("#" + datval).css({ "display": "block" });
        }
    });
    $("#Button_SearchCustomer").click(function () {
        SlideContent($("#customerList"));
        SearchCustomer();
        ShowLoader();
    });
    $("#Button_AddCustomer").click(function () {
        if (ValidateControlList($("#popupCustomerAdd"))) {
            SlideContent($("#customerDetail"));
            InsertCustomer();
        }
    });
    $("#additionalBtnCustomer").click(function () {
        ListRecordType("customer", "insert");
        ListRecordName("customer", "insert");
        ShowPopup("popupCustomerLovsAdd");
    });

    $("#Button_AddCustomerLovs").click(function () {
        if (ValidateControlList($("#popupCustomerLovsAdd"))) {
            AddCustomerLovs();
        }
    });
    $("#Button_UpdateCustomerLovs").click(function () {
        if (ValidateControlList($("#popupCustomerLovsUpdate"))) {
            UpdateCustomerLovs();
        }
    });

    $("#Button_ShowAllClients").click(function () {
        GetCustomerClients();
        SlideContent($("#clientList"));
        ShowLoader();
    });

    $(".backButton").click(function () {
        SlideContent($("#" + $(this).attr("data").toString()));
    });

    $("#Button_SearchClient").click(function () {
        SearchClient();
        SlideContent($("#clientList"));
        ShowLoader();
    });

    $("#Button_AddClient").click(function () {
        if (ValidateControlList($("#popupClientAdd"))) {
            InsertClient();
            SlideContent($("#clientDetail"));
        }
    });
    $("#additionalBtnClient").click(function () {
        ListRecordType("client", "insert");
        ListRecordName("client", "insert");
        ShowPopup("popupClientLovsAdd");
    });

    $("#addBtnRequest").click(function () {
        ListRequestType();
        ShowPopup("popupClientRequest");

    });

    $("#Button_AddClientLovs").click(function () {
        if (ValidateControlList($("#popupClientLovsAdd"))) {
            AddClientLovs();
        }
    });
    $("#Button_UpdateClientLovs").click(function () {
        if (ValidateControlList($("#popupClientLovsUpdate"))) {
            UpdateClientLovs();
        }
    });

    $("#Button_AddRequest").click(function () {
        if (ValidateControlList($("#popupClientRequest"))) {
            AddRequest();
        }
    });
    $("#Button_StartWfItem").click(function () {        
            ConfirmWfItem();
            $(".pop").fadeOut();        
    });

    $("#popupBg").click(function () {
        $(".pop").fadeOut();
        ResetInput($(".pop"));
    });
    $(document).click(function (event) {
        var target = $(event.target);
        if ($(target).attr("class") != undefined) {
            if ($(target).attr("class").toString().indexOf("optbutton") == -1) {
                $(".optionUl").css({ "display": "none" });
            }
            //            if ($(target).attr("class").toString().indexOf("additionalInfoListItem") == -1) {
            //                $(".additionalInfoListItem").removeClass("selected");
            //                $(".additionalInfoListItem").find(".itemMenuContainer").css({ "display": "none" });
            //            }
            //            if ($(target).attr("class").toString().indexOf("titleContainer") == -1) {
            //                $(".titleContainer").removeClass("selected");
            //                $(".titleContainer").find(".itemMenuContainer").css({ "display": "none" });
            //            }
        }
        else {
            $(".optionUl").css({ "display": "none" });
        }
    });

    $("input:text").keyup(function (event) {
        if (event.keyCode == 13) {
            var list = $($(this).parents("table")[0]).find(".submit").filter(function () {
                return $(this).css("display") == "block";
            });          
            $(list).click();
        }
    });
}

function SetContentPosition() {
    var ActiveContent = $(".content.active");
    var List = $(".mainContainer>div");
    var Index = $(List).index(ActiveContent);
    var width = $(ActiveContent).width();
    for (var i = 0; i < List.length; i++) {
        var Left;
        Left = ((i - Index) * width);
        $(List[i]).css({ "left": Left + "px" });
        if (Left == 0) {
            Left = -Left;
            $(List[i]).css({ "right": "0px" });
        }
        else {
            Left = -Left;
            $(List[i]).css({ "right": Left + "px" });
        }
    }
}

function SlideContent(element) {
    var ActiveContent = $(".content.active");
    var List = $(".mainContainer>div");
    var ActiveIndex = $(List).index(ActiveContent);
    var Index = $(List).index(element);
    var width = $(ActiveContent).width();
    for (var i = 0; i < List.length; i++) {
        if (ActiveIndex > Index) {
            $(List[i]).animate({ "right": "-=" + ((ActiveIndex - Index) * width) + "px", "left": "+=" + ((ActiveIndex - Index) * width) + "px" });

        }
        if (ActiveIndex < Index) {
            $(List[i]).animate({ "right": "-=" + ((ActiveIndex - Index) * width) + "px", "left": "+=" + ((ActiveIndex - Index) * width) + "px" });

        }
    }
    $(".content").removeClass("active");
    $(element).addClass("active");
}

// *****************Region Common

function ListRecordType(userType, eventType) {

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "default.aspx/ListRecordType",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                if (userType == "customer" & eventType == "insert") SetRecordtTypeCustomer(result);
                if (userType == "customer" & eventType == "edit") SetRecordtTypeCustomerEdit(result);
                if (userType == "customer" & eventType == "group") GroupCustomerLov(result);
                if (userType == "client" & eventType == "insert") SetRecordtTypeClient(result);
                if (userType == "client" & eventType == "edit") SetRecordtTypeClientEdit(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function ListRecordName(userType, eventType) {
    //    var SelectedValue = $('#Select_RecordType').val();
    //    var list = [SelectedValue];
    //    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        //        data: jsonText,
        url: "default.aspx/ListRecordName",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                if (userType == "customer" & eventType == "insert") SetRecordtNameCustomer(result);
                if (userType == "customer" & eventType == "edit") SetRecordtNameCustomerEdit(result);
                if (userType == "client" & eventType == "insert") SetRecordtNameClient(result);
                if (userType == "client" & eventType == "edit") SetRecordtNameClientEdit(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function ListRequestType() {

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "default.aspx/ListRequestType",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetRequestTypeClient(result);
            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

// *****************RegionEnd Common

// *****************Region Customer

function SearchCustomer() {
    var keyword = $("#Text_Search").val();
    var list = [keyword];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/SearchCustomer",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                ListCustomer(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}


function ListCustomer(result) {
    var obj = jQuery.parseJSON(result);
    var customerList = $("#customerList");
    $(customerList).empty();
    for (var i = 0; i < obj.length; i++) {
        var ItemContainer = document.createElement("div");
        var Item = document.createElement("div");
        var h2 = document.createElement("h2");
        var span = document.createElement("span");
        var CustomerName = obj[i].CustomerName;
        var CustomerId = obj[i].CustomerId;
        var lovs = obj[i].LOVs;
        //        var Address;
        //        var Phone;
        //        for (var k = 0; k < lovs.length; k++) {
        //            lovsItem = lovs[k];
        //            if (lovsItem.RecordType == "0" & lovsItem.RecordName == "5") {
        //                Address = lovsItem.RecordValue; //lovsItem.Description + ": " + 
        //            }
        //            if (lovsItem.RecordType == "0" & lovsItem.RecordName == "4") {
        //                Phone = lovsItem.RecordValue; //lovsItem.Description + ": " + 
        //            }
        //        }
        //        $(span).append(Address + "<br>" + Phone);
        $(h2).append(CustomerName);
        $(Item).append(h2);
        //        $(Item).append(span);
        $(ItemContainer).append(Item);
        $(ItemContainer).attr("data", CustomerId);
        $(ItemContainer).addClass("ResultListItemContainer");
        $(ItemContainer).click(function () {
            var CustomerId = $(this).attr("data");
            $("#Hidden_CustomerId").val(CustomerId);
            SlideContent($("#customerDetail"));
            GetCustomerDetail("view");
            ShowLoader();
        });
        $(customerList).append(ItemContainer);
    }
    if (obj.length == 0) {
        var ItemContainer = document.createElement("div");
        var Item = document.createElement("div");
        var h2 = document.createElement("h2");
        var span = document.createElement("span");
        $(h2).append("No results found");
        $(Item).append(h2);
        $(ItemContainer).append(Item);
        $(ItemContainer).addClass("ResultListItemContainer");
        $(customerList).append(ItemContainer);
    }
    SetScroll($("#customerList"));
    HideLoader();
}

function InsertCustomer() {
    var CustomerName = $("#Text_CustomerName").val();
    var CustomerCode = $("#Text_CustomerCode").val();
    var list = [CustomerName, CustomerCode];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/InsertCustomer",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                $("#Hidden_CustomerId").val(result);
                GetCustomerDetail("view");
                ResetInput($("#popupCustomerAdd"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });

}

function GetCustomerDetail(event) {
    var CustomerId = $("#Hidden_CustomerId").val();
    var list = [CustomerId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/GetCustomerDetail",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                if (event == "view") {
                    SetCustomerDetail(result);
                }
                if (event == "edit") {
                    SetCustomerEdit(result);
                }
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function SetCustomerEdit(result) {
    var obj = jQuery.parseJSON(result);
    var Title = $("#Text_CustomerNameEdit").val(obj.CustomerName);
    $("#Button_EditCustomer").attr("data", obj.CustomerId);
    $("#Button_EditCustomer").unbind("click");
    $("#Button_EditCustomer").click(function () {
        if (ValidateControlList($("#popupCustomerEdit"))) {
            UpdateCustomer();
        }
    });
    HideLoader();
}

function UpdateCustomer() {
    var CustomerId = $("#Hidden_CustomerId").val();
    var CustomerName = $("#Text_CustomerNameEdit").val();
    var list = [CustomerId, CustomerName];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/UpdateCustomer",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetCustomerDetail("view");
                ResetInput($("#popupCustomerEdit"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });

}

function SetCustomerDetail(result) {
    ListRecordType("customer", "edit");
    ListRecordName("customer", "edit");
    var obj = jQuery.parseJSON(result);
    var Title = $("#CustomerTitle").find("h2");
    var TitlDesc = $("#CustomerTitle").find("span");
    var TitleCont = $("#CustomerTitle").parent();
    $("#customerEdit").unbind("click");
    $("#customerEdit").click(function () {
        GetCustomerDetail("edit");
        ShowPopup("popupCustomerEdit");
    });
    $("#customerDelete").unbind("click");
    $("#customerDelete").click(function () {
        SetConfirm("customer", "This data can not be restored. Are you sure?");
    });
    $(Title).empty();
    $(Title).append(obj.CustomerName);
    $(Title).attr("data", obj.CustomerId);
    $(TitleCont).unbind("click");

    $(TitleCont).click(function () {
        if (this.className.indexOf("selected") == "-1") {
            var menucontainer = $(this).find(".itemMenuContainer");
            $(menucontainer).fadeIn();
            $(this).addClass("selected");
        }
        else {
            var menucontainer = $(this).find(".itemMenuContainer");
            $(menucontainer).fadeOut();
            $(this).removeClass("selected");
        }

    });
    var Address = "";
    var Phone = "";
    $("#customerInfoList").empty();
    var lovs = obj.LOVs;
    var arrRecordType = new Array();
    var indexNum = 0;
    for (var p = 0; p < lovs.length; p++) {
        if (arrRecordType.indexOf(lovs[p].RecordTypeString) == -1) {
            arrRecordType[indexNum] = lovs[p].RecordTypeString;
            indexNum = indexNum + 1;
            var TypeContainer = document.createElement("div");
            var TypeContainerDefCont = document.createElement("h3");
            var TypeContainerDef = document.createElement("a");
            TypeContainerDef.href = "#";
            $(TypeContainerDef).append(lovs[p].RecordTypeString);
            $(TypeContainerDefCont).append(TypeContainerDef);
            $(TypeContainer).attr("id", "customerLOV_" + lovs[p].RecordTypeString);
            $(TypeContainer).addClass("accordionContent");
            $("#customerInfoList").append(TypeContainerDefCont);
            $("#customerInfoList").append(TypeContainer);
        }
    }

    for (var k = 0; k < lovs.length; k++) {
        var ItemContainer = document.createElement("div");
        var Item = document.createElement("div");
        var ItemMenu = document.createElement("div");
        var ItemMenuDelete = document.createElement("div");
        var ItemMenuEdit = document.createElement("div");
        var inputDesc = document.createElement("input");
        $(inputDesc).attr("type", "hidden");
        var inputVal = document.createElement("input");
        $(inputVal).attr("type", "hidden");
        $(Item).addClass("item");
        $(ItemContainer).addClass("additionalInfoListItem");
        $(ItemMenu).addClass("itemMenuContainer");
        $(ItemMenuDelete).addClass("itemMenu delete");
        $(ItemMenuEdit).addClass("itemMenu edit");
        lovsItem = lovs[k];
        if (lovsItem.RecordType == "0" & lovsItem.RecordNameString == "Address") {
            Address = lovsItem.RecordValue; //lovsItem.Description + ": " + 
        }
        if (lovsItem.RecordType == "0" & lovsItem.RecordNameString == "Phone") {
            Phone = lovsItem.RecordValue; //lovsItem.Description + ": " + 
        }

        var Record;
        $(inputDesc).attr("id", "customer_lovs_desc_" + lovsItem.RecId);
        $(inputVal).attr("id", "customer_lovs_val_" + lovsItem.RecId);
        $(inputDesc).val(lovsItem.Description);
        $(inputVal).val(lovsItem.RecordValue);
        Record = lovsItem.Description + ": " + lovsItem.RecordValue;
        $(Item).attr("data", lovsItem.RecordType + "_" + lovsItem.RecordName);
        $(Item).append(Record);
        $(Item).append(inputDesc);
        $(Item).append(inputVal);
        $(ItemMenuEdit).attr("data", lovsItem.RecId);
        $(ItemMenuDelete).attr("data", lovsItem.RecId);
        $(ItemMenuDelete).click(function () {
            $("#Hidden_DeleteId").val($(this).attr("data"));
            SetConfirm("customerLovs", "This data can not be restored. Are you sure?");
        });
        $(ItemMenuEdit).click(function () {
            $("#Hidden_RecId").val($(this).attr("data"));
            var recordTypeName = $($(this).parents(".item")[0]).attr("data").split("_");
            var recordType = recordTypeName[0];
            var recordName = recordTypeName[1];
            var recordDesc = $("#customer_lovs_desc_" + $(this).attr("data"));
            var recordVal = $("#customer_lovs_val_" + $(this).attr("data"));
            $("#SelectCustomer_RecordTypeEdit").val(recordType);
            $("#SelectCustomer_RecordNameEdit").val(recordName);
            $("#TextCustomer_RecordDescriptionEdit").val($(recordDesc).val());
            $("#TextCustomer_RecordValueEdit").val($(recordVal).val());
            ShowPopup("popupCustomerLovsUpdate");
        });
        $(ItemMenuDelete).click(function () {
            //ShowPopup("#popupCustomerLovsUpdate");
        });
        $(ItemMenu).append(ItemMenuDelete);
        $(ItemMenu).append(ItemMenuEdit);
        $(Item).append(ItemMenu);
        $(ItemContainer).append(Item);
        $(ItemContainer).unbind("click");
        $(ItemContainer).click(function () {
            if (this.className.indexOf("selected") == "-1") {
                var menucontainer = $(this).find(".itemMenuContainer");
                $(".additionalInfoListItem").removeClass("selected");
                $(".additionalInfoListItem").find(".itemMenuContainer").css({ "display": "none" });
                $(menucontainer).css({ "display": "block" });
                $(this).addClass("selected");
            }
            else {
                var menucontainer = $(this).find(".itemMenuContainer");
                $(menucontainer).css({ "display": "none" });
                $(".additionalInfoListItem").removeClass("selected");
                $(this).removeClass("selected");
            }

        });
        $("#customerLOV_" + lovsItem.RecordTypeString).append(ItemContainer);

    }
    $(TitlDesc).empty();
    $(TitlDesc).append(Address + " " + Phone);
    $(".accordionContent").append("<br style='clear:both;'>");
    $("#customerDetail").mCustomScrollbar("update");
    $("#customerInfoList").accordion("destroy");
    $("#customerInfoList").accordion({ collapsible: true, autoHeight: false, active: -1 });
    HideLoader();
}



function SetRecordtTypeCustomer(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectCustomer_RecordType').empty();
    $('#SelectCustomer_RecordType').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectCustomer_RecordType').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtNameCustomer(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectCustomer_RecordName').empty();
    $('#SelectCustomer_RecordName').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectCustomer_RecordName').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtTypeCustomerEdit(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectCustomer_RecordTypeEdit').empty();
    $('#SelectCustomer_RecordTypeEdit').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectCustomer_RecordTypeEdit').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtNameCustomerEdit(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectCustomer_RecordNameEdit').empty();
    $('#SelectCustomer_RecordNameEdit').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectCustomer_RecordNameEdit').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function AddCustomerLovs() {
    var CustomerId = $("#Hidden_CustomerId").val();
    var RecordType = $("#SelectCustomer_RecordType").val();
    var RecordName = $("#SelectCustomer_RecordName").val();
    var RecordDescription = $("#TextCustomer_RecordDescription").val();
    var RecordValue = $("#TextCustomer_RecordValue").val();
    var CustomerOrClient = "Customer";
    var list = [CustomerId, RecordType, RecordName, RecordDescription, RecordValue, CustomerOrClient];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/AddLovs",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetCustomerDetail("view");
                ResetInput($("#popupCustomerLovsAdd"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function UpdateCustomerLovs() {
    var CustomerId = $("#Hidden_CustomerId").val();
    var RecId = $("#Hidden_RecId").val();
    var RecordType = $("#SelectCustomer_RecordTypeEdit").val();
    var RecordName = $("#SelectCustomer_RecordNameEdit").val();
    var RecordDescription = $("#TextCustomer_RecordDescriptionEdit").val();
    var RecordValue = $("#TextCustomer_RecordValueEdit").val();
    var list = [CustomerId, RecordType, RecordName, RecordDescription, RecordValue, RecId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/UpdateLovs",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetCustomerDetail("view");
                ResetInput($("#popupCustomerLovsUpdate"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function DeleteCustomer() {
    var CustomerId = $("#Hidden_CustomerId").val();
    var list = [CustomerId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/DeleteCustomer",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SlideContent($("#dashBoard"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function DeleteCustomerLovs() {
    var CustomerId = $("#Hidden_CustomerId").val();
    var RecId = $("#Hidden_DeleteId").val();
    var list = [CustomerId, RecId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/DeleteLovs",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetCustomerDetail("view");
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

// *****************RegionEnd Customer

// *****************Region Client

function SearchClient() {
    var keyword = $("#Text_Search").val();
    var list = [keyword];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/SearchClient",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                ListClient(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}


function GetCustomerClients() {
    var CustomerId = $("#Hidden_CustomerId").val();
    var list = [CustomerId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/ListCustomerClients",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                ListClient(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}


function ListClient(result) {
    var obj = jQuery.parseJSON(result);
    var clientList = $("#clientList");
    $(clientList).empty();
    for (var i = 0; i < obj.length; i++) {
        var ItemContainer = document.createElement("div");
        var Item = document.createElement("div");
        var h2 = document.createElement("h2");
        var span = document.createElement("span");
        var ClientName = obj[i].ClientName;
        var ClientId = obj[i].ClientId;
                var customerName = "CustomerName"//obj[i].Customer.CustomerName;
        //        var Address = "";
        //        var Phone ="";
        //        for (var k = 0; k < lovs.length; k++) {
        //            lovsItem = lovs[k];
        //            if (lovsItem.RecordType == "0" & lovsItem.RecordName == "5") {
        //                Address = lovsItem.RecordValue; //lovsItem.Description + ": " + 
        //            }
        //            if (lovsItem.RecordType == "0" & lovsItem.RecordName == "4") {
        //                Phone = lovsItem.RecordValue; //lovsItem.Description + ": " + 
        //            }
        //        }
        $(span).append(customerName);
        $(h2).append(ClientName);
        $(Item).append(h2);
        $(Item).append(span);
        $(ItemContainer).append(Item);
        $(ItemContainer).attr("data", ClientId);
        $(ItemContainer).addClass("ResultListItemContainer");
        $(ItemContainer).click(function () {
            var ClientId = $(this).attr("data");
            $("#Hidden_ClientId").val(ClientId);
            SlideContent($("#clientDetail"));
            GetClientDetail("view");
            GetClientWFList();
            ShowLoader();
        });
        $(clientList).append(ItemContainer);
    }
    //$("#clientList").mCustomScrollbar("update");
    if (obj.length == 0) {
        var ItemContainer = document.createElement("div");
        var Item = document.createElement("div");
        var h2 = document.createElement("h2");
        var span = document.createElement("span");
        $(h2).append("No results found");
        $(Item).append(h2);
        $(ItemContainer).append(Item);
        $(ItemContainer).addClass("ResultListItemContainer");
        $(clientList).append(ItemContainer);
    }
    SetScroll($("#clientList"));
    HideLoader();
}

function GetCustomerList() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "default.aspx/ListCustomer",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetCustomerList(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}


function SetCustomerList(result) {
    var obj = jQuery.parseJSON(result);
    $('#Select_AddClientCustomerList').empty();
    $('#Select_AddClientCustomerList').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#Select_AddClientCustomerList').append($('<option/>').attr("value", option.CustomerId).text(option.CustomerName));
    });
    HideLoader();
}

function InsertClient() {
    var CustomerId = $("#Select_AddClientCustomerList").val();
    var ClientName = $("#Text_AddClientName").val();
    var ClientCode = $("#Text_AddClientCode").val();
    var list = [CustomerId, ClientName, ClientCode];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/InsertClient",
        success: function (response) {
            //****************************************
            var result = response.d;
            $("#Hidden_ClientId").val(result);
            GetClientDetail("view");
            ResetInput($("#popupClientAdd"));
            $(".pop").fadeOut();
            if (result != "0") {

            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });

}

function GetClientDetail(event) {
    var ClientId = $("#Hidden_ClientId").val();
    var list = [ClientId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/GetClientDetail",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                if (event == "view") {
                    SetClientDetail(result);
                }
                if (event == "edit") {
                    SetClientEdit(result);
                }
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function GetClientWFList() {
    var ClientId = $("#Hidden_ClientId").val();
    var list = [ClientId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/GetClientWFList",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetClientWfList(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function GetWfItemList() {
    var ClientId = $("#Hidden_ClientId").val();
    var WfId = $("#Hidden_WfId").val();
    var list = [ClientId, WfId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/GetWfItemList",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SetWfItemList(result);
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function SetClientEdit(result) {
    var obj = jQuery.parseJSON(result);
    var Title = $("#Text_ClientEditName").val(obj.ClientName);
    $("#Button_EditClient").attr("data", obj.ClientId);
    $("#Hidden_CustomerId").val(obj.Customer.CustomerId);
    $("#Button_EditClient").unbind("click");
    $("#Button_EditClient").click(function () {
        if (ValidateControlList($("#popupClientEdit"))) {
            UpdateClient();
        }
    })
    HideLoader();
}

function UpdateClient() {
    var ClientId = $("#Hidden_ClientId").val();
    var ClientName = $("#Text_ClientEditName").val();
    var CustomerId = $("#Hidden_CustomerId").val();
    var list = [ClientId, ClientName, CustomerId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/UpdateClient",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetClientDetail("view");
                ResetInput($("#popupClientEdit"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });

}

function SetClientDetail(result) {
    ListRecordType("client", "edit");
    ListRecordName("client", "edit");
    var obj = jQuery.parseJSON(result);
    var Title = $("#ClientTitle").find("h2");
    var TitlDesc = $("#ClientTitle").find("span");
    var TitleCont = $("#ClientTitle").parent();
    $("#clientEdit").unbind("click");
    $("#clientEdit").click(function () {
        GetClientDetail("edit");
        ShowPopup("popupClientEdit");
    });
    $("#clientDelete").unbind("click");
    $("#clientDelete").click(function () {
        SetConfirm("client", "This data can not be restored. Are you sure?");
    });
    $(Title).empty();
    $(Title).append(obj.ClientName);
    $(Title).attr("data", obj.CustomerId);
    $(TitleCont).unbind("click");

    $(TitleCont).click(function () {
        if (this.className.indexOf("selected") == "-1") {
            var menucontainer = $(this).find(".itemMenuContainer");
            $(menucontainer).fadeIn();
            $(this).addClass("selected");
        }
        else {
            var menucontainer = $(this).find(".itemMenuContainer");
            $(menucontainer).fadeOut();
            $(this).removeClass("selected");
        }

    });
    var Address;
    var Phone;
    $("#clientInfoList").empty();
    var lovs = obj.LOVs;
    var CustomerName = obj.Customer.CustomerName;
    var arrRecordType = new Array();
    var indexNum = 0;
    for (var p = 0; p < lovs.length; p++) {
        if (arrRecordType.indexOf(lovs[p].RecordTypeString) == -1) {
            arrRecordType[indexNum] = lovs[p].RecordTypeString;
            indexNum = indexNum + 1;
            var TypeContainer = document.createElement("div");
            var TypeContainerDefCont = document.createElement("h3");
            var TypeContainerDef = document.createElement("a");
            TypeContainerDef.href = "#";
            $(TypeContainerDef).append(lovs[p].RecordTypeString);
            $(TypeContainerDefCont).append(TypeContainerDef);
            $(TypeContainer).attr("id", "clientLOV_" + lovs[p].RecordTypeString);
            $(TypeContainer).addClass("accordionContent");
            $("#clientInfoList").append(TypeContainerDefCont);
            $("#clientInfoList").append(TypeContainer);
        }
    }

    for (var k = 0; k < lovs.length; k++) {
        var ItemContainer = document.createElement("div");
        var Item = document.createElement("div");
        var ItemMenu = document.createElement("div");
        var ItemMenuDelete = document.createElement("div");
        var ItemMenuEdit = document.createElement("div");
        var inputDesc = document.createElement("input");
        $(inputDesc).attr("type", "hidden");
        var inputVal = document.createElement("input");
        $(inputVal).attr("type", "hidden");
        $(Item).addClass("item");
        $(ItemContainer).addClass("additionalInfoListItem");
        $(ItemMenu).addClass("itemMenuContainer");
        $(ItemMenuDelete).addClass("itemMenu delete");
        $(ItemMenuEdit).addClass("itemMenu edit");
        lovsItem = lovs[k];
//        if (lovsItem.RecordType == "0" & lovsItem.RecordName == "5") {
//            Address = lovsItem.RecordValue; //lovsItem.Description + ": " + 
//        }
//        if (lovsItem.RecordType == "0" & lovsItem.RecordName == "4") {
//            Phone = lovsItem.RecordValue; //lovsItem.Description + ": " + 
//        }

        var Record;
        $(inputDesc).attr("id", "client_lovs_desc_" + lovsItem.RecId);
        $(inputVal).attr("id", "client_lovs_val_" + lovsItem.RecId);
        $(inputDesc).val(lovsItem.Description);
        $(inputVal).val(lovsItem.RecordValue);
        Record = lovsItem.Description + ": " + lovsItem.RecordValue;
        $(Item).attr("data", lovsItem.RecordType + "_" + lovsItem.RecordName);
        $(Item).append(Record);
        $(Item).append(inputDesc);
        $(Item).append(inputVal);
        $(ItemMenuEdit).attr("data", lovsItem.RecId);
        $(ItemMenuDelete).attr("data", lovsItem.RecId);
        $(ItemMenuDelete).click(function () {
            $("#Hidden_DeleteId").val($(this).attr("data"));
            SetConfirm("clientLovs", "This data can not be restored. Are you sure?");
        });
        $(ItemMenuEdit).click(function () {
            $("#Hidden_RecId").val($(this).attr("data"));
            var recordTypeName = $($(this).parents(".item")[0]).attr("data").split("_");
            var recordType = recordTypeName[0];
            var recordName = recordTypeName[1];
            var recordDesc = $("#client_lovs_desc_" + $(this).attr("data"));
            var recordVal = $("#client_lovs_val_" + $(this).attr("data"));
            $("#SelectClient_RecordTypeEdit").val(recordType);
            $("#SelectClient_RecordNameEdit").val(recordName);
            $("#TextClient_RecordDescriptionEdit").val($(recordDesc).val());
            $("#TextClient_RecordValueEdit").val($(recordVal).val());
            ShowPopup("popupClientLovsUpdate");
        });
        $(ItemMenuDelete).click(function () {
            //ShowPopup("#popupCustomerLovsUpdate");
        });
        $(ItemMenu).append(ItemMenuDelete);
        $(ItemMenu).append(ItemMenuEdit);
        $(Item).append(ItemMenu);
        $(ItemContainer).append(Item);
        $(ItemContainer).unbind("click");
        $(ItemContainer).click(function () {
            if (this.className.indexOf("selected") == "-1") {
                var menucontainer = $(this).find(".itemMenuContainer");
                $(".additionalInfoListItem").removeClass("selected");
                $(".additionalInfoListItem").find(".itemMenuContainer").css({ "display": "none" });
                $(menucontainer).css({ "display": "block" });
                $(this).addClass("selected");
            }
            else {
                var menucontainer = $(this).find(".itemMenuContainer");
                $(menucontainer).css({ "display": "none" });
                $(".additionalInfoListItem").removeClass("selected");
                $(this).removeClass("selected");
            }

        });
        $("#clientLOV_" + lovsItem.RecordTypeString).append(ItemContainer);
    }
    $(TitlDesc).empty();
    $(TitlDesc).append(CustomerName);
    $(".accordionContent").append("<br style='clear:both;'>");
    $("#clientDetail").mCustomScrollbar("update");
    $("#clientInfoList").accordion("destroy");
    $("#clientInfoList").accordion({ collapsible: true, autoHeight: false, active: -1 });
    HideLoader();
}

function SetClientWfList(result) {
    $("#clientRequestList").empty();
    //    var obj = jQuery.parseJSON(result);
    //    for (var i = 0; i < obj.length; i++) {
    //        var ItemContainer = document.createElement("div");
    //        $(ItemContainer).addClass("additionalInfoListItem");
    //        $(ItemContainer).append(obj[i].ProjectCode);
    //        $("#clientRequestList").append(ItemContainer);
    //    }

    var ItemContainer = document.createElement("div");
    var label = document.createElement("h2");
    var datespan = document.createElement("span");
    $(ItemContainer).addClass("additionalInfoListItem");
    $(label).append("PMI-JFK-RP-001");
    $(datespan).append("13-01-2013 07:00");
    $(ItemContainer).append(label);
    $(ItemContainer).append(datespan);
    $(ItemContainer).attr("data", "74");
    $(ItemContainer).click(function () {
        var ClientName = $("#ClientTitle h2").html();
        var ClientAdditonal = $("#ClientTitle span").html();
        var ProjectCode = $(this).find("h2").html();
        var RecId = $(this).attr("data");
        $("#Hidden_WfId").val(RecId);
        $("#wfTitle h2").html(ClientName);
        $("#wfTitle span").html(ClientAdditonal);
        $("#wfProjectHistory").html("Workflow History (" + ProjectCode + ")");
        GetWfItemList();
        SlideContent($("#wfDetail"));
        $("#WFHistory").empty();
        var histFirst = document.createElement("div");
        var histFirstH = document.createElement("h2");
        $(histFirst).addClass("wfHistoryListItem");
        $(histFirstH).append("Workflow has not started");
        $(histFirst).append(histFirstH);
        $("#WFHistory").append(histFirst);

    });
    $("#clientRequestList").append(ItemContainer);
}

function SetWfItemList(result) {
    $("#WFItemList").empty();
    var obj = jQuery.parseJSON(result);
    $("#wfTypeName").html("Workflow Items (RP)");
    for (var i = 0; i < obj.length; i++) {
        var ItemContainer = document.createElement("div");
        if (i == 0) {
            $(ItemContainer).addClass("wfListItem queue");
            wfItemClick(ItemContainer);
        }
        else $(ItemContainer).addClass("wfListItem");
        $(ItemContainer).append(obj[i]);
        $("#WFItemList").append(ItemContainer);
//        $(ItemContainer).append($(divList[WfIndex]).html());
//        $("#WFHistory").append(ItemContainer);
    }
    $("#wfDetail").mCustomScrollbar("update");
}

function ConfirmWfItem() {
    var divList = $("#WFItemList div");
    var WfIndex = $("#Hidden_WfIndex").val();
    WfIndex = parseInt(WfIndex);
    var item = divList[WfIndex];
    var ItemContainer = document.createElement("div");
    var hel = document.createElement("h2");
    var hspan = document.createElement("span");
    var hspanDate = document.createElement("span");
    $(hspanDate).addClass("date");
    $(ItemContainer).addClass("wfHistoryListItem");
    if (item.className.indexOf("queue") != -1) {
        wfItemClick(divList[WfIndex + 1]);
        var ItemClass = item.className;
        if (WfIndex == "0" & ItemClass.indexOf("queue") != -1) {
            $("#WFHistory").empty();
        }
        var Comment = $("#Text_WfComment").val();
        if (Comment == "Comment") {
            Comment = "";
        }
        var d = new Date();
        var curr_date = ("0" + d.getDate()).slice(-2);
        var curr_month = ("0" + d.getMonth()).slice(-2);
        var curr_year = d.getFullYear();
        var curr_hour = ("0" + d.getHours()).slice(-2);
        var curr_min = ("0" + d.getMinutes()).slice(-2);
        $(hspan).append(Comment);
        $(hspanDate).html(curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour +":"+curr_min);
        $(hel).append($(divList[WfIndex]).html());
        $(ItemContainer).append(hel);
        $(ItemContainer).append(hspan);
        $(ItemContainer).append(hspanDate);
        $("#WFHistory").append(ItemContainer);
        $(divList[WfIndex]).removeClass("queue");
        $(divList[WfIndex]).addClass("completed");
        $(divList[WfIndex + 1]).addClass("queue");
    }
    else {
        for (var i = WfIndex + 1; i < divList.length; i++) {
            $(divList[i]).unbind("click");
            $(divList[i]).removeClass("completed");
            $(divList[i]).removeClass("queue");
        }
        $(divList[WfIndex + 1]).addClass("queue");
        wfItemClick(divList[WfIndex + 1]);
        var Comment = $("#Text_WfComment").val();
        if (Comment == "Comment") {
            Comment = "";
        }
        var d = new Date();
        var curr_date = ("0" + d.getDate()).slice(-2);
        var curr_month = ("0" + d.getMonth()).slice(-2);
        var curr_year = d.getFullYear();
        var curr_hour = ("0" + d.getHours()).slice(-2);
        var curr_min = ("0" + d.getMinutes()).slice(-2);
        $(hspan).append(Comment);
        $(hspanDate).html(curr_date + "-" + curr_month + "-" + curr_year + " " + curr_hour + ":" + curr_min);
        $(hel).append($(divList[WfIndex]).html());
        $(ItemContainer).append(hel);
        $(ItemContainer).append(hspan);
        $(ItemContainer).append(hspanDate);
        $("#WFHistory").append(ItemContainer);
    }
    $("#wfDetail").mCustomScrollbar("update");
}

function wfItemClick(element) {
    $(element).click(function () {
        ShowPopup("popupWFItem");
        ResetInput($("#popupWFItem"));
        var divList = $("#WFItemList div");
        var index = divList.index(this);
        $("#Hidden_WfIndex").val(index);
    });

}

function SetRecordtTypeClient(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectClient_RecordType').empty();
    $('#SelectClient_RecordType').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectClient_RecordType').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtNameClient(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectClient_RecordName').empty();
    $('#SelectClient_RecordName').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectClient_RecordName').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtTypeClientEdit(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectClient_RecordTypeEdit').empty();
    $('#SelectClient_RecordTypeEdit').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectClient_RecordTypeEdit').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function SetRecordtNameClientEdit(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectClient_RecordNameEdit').empty();
    $('#SelectClient_RecordNameEdit').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectClient_RecordNameEdit').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

function AddClientLovs() {
    var ClientId = $("#Hidden_ClientId").val();
    var RecordType = $("#SelectClient_RecordType").val();
    var RecordName = $("#SelectClient_RecordName").val();
    var RecordDescription = $("#TextClient_RecordDescription").val();
    var RecordValue = $("#TextClient_RecordValue").val();
    var CustomerOrClient = "Client";
    var list = [ClientId, RecordType, RecordName, RecordDescription, RecordValue, CustomerOrClient];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/AddLovs",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetClientDetail("view");
                ResetInput($("#popupClientLovsAdd"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function AddRequest() {
    var ClientId = $("#Hidden_ClientId").val();
    var RequestType = $("#SelectClient_Request").val();
    var list = [ClientId, RequestType];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/AddRequest",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetClientWFList();
                ResetInput($("#popupClientRequest"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function UpdateClientLovs() {
    var ClientId = $("#Hidden_ClientId").val();
    var RecId = $("#Hidden_RecId").val();
    var RecordType = $("#SelectClient_RecordTypeEdit").val();
    var RecordName = $("#SelectClient_RecordNameEdit").val();
    var RecordDescription = $("#TextClient_RecordDescriptionEdit").val();
    var RecordValue = $("#TextClient_RecordValueEdit").val();
    var list = [ClientId, RecordType, RecordName, RecordDescription, RecordValue, RecId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/UpdateLovs",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetClientDetail("view");
                ResetInput($("#popupClientLovsUpdate"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function DeleteClient() {
    var ClientId = $("#Hidden_ClientId").val();
    var list = [ClientId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/DeleteClient",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                SlideContent($("#dashBoard"));
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function DeleteClientLovs() {
    var ClientId = $("#Hidden_Client").val();
    var RecId = $("#Hidden_DeleteId").val();
    var list = [ClientId, RecId];
    var jsonText = JSON.stringify({ list: list });
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: jsonText,
        url: "default.aspx/DeleteLovs",
        success: function (response) {
            var result = response.d;
            if (result != "0") {
                GetClientDetail("view");
                $(".pop").fadeOut();
            }
            else {

            }
        },
        error: function () {
            alert('Talep esnasında sorun oluştu. Yeniden deneyin');
        }
    });
}

function SetRequestTypeClient(result) {
    var obj = jQuery.parseJSON(result);
    $('#SelectClient_Request').empty();
    $('#SelectClient_Request').append($('<option/>').attr("value", "Select").text("Select"));
    $.each(obj, function (i, option) {
        $('#SelectClient_Request').append($('<option/>').attr("value", option.KF).text(option.VF));
    });
}

// *****************RegionEnd Client